package com.chis.dubbo;

import com.chis.jm.pojo.comm.Result;
import net.sf.json.JsonConfig;
import net.sf.json.processors.DefaultDefaultValueProcessor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Date;


/**
 * 
 * @Description 返回数据工具类
 * @Company zwx
 * @author yc
 * @version 2018年03月27日
 */
public class ResultUtil {

	/** 日志 */
	private static Logger LOG = LogManager.getLogger(ResultUtil.class);

	/**
	 * 返回成功
	 * @param object 返回信息
	 * @return Result
	 */
	public static String success(Object object) {
		Result result = new Result();
		result.setSuccess(true);
		result.setMsg("返回成功");
		result.setData(object);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
		jsonConfig.registerDefaultValueProcessor(Integer.class, new DefaultDefaultValueProcessor() {
			public Object getDefaultValue(Class type) {
				return null;
			}
		});
		jsonConfig.registerDefaultValueProcessor(Long.class, new DefaultDefaultValueProcessor() {
			public Object getDefaultValue(Class type) {
				return null;
			}
		});
		net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(result,jsonConfig);
		LOG.error("json:" + json.toString());
		return json.toString();
	}

	/**
	 * 返回成功
	 * @return Result
	 */
	public static String success() {
		Result result = new Result();
		result.setSuccess(true);
		result.setMsg("返回成功");
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
		jsonConfig.registerDefaultValueProcessor(Integer.class, new DefaultDefaultValueProcessor() {
			public Object getDefaultValue(Class type) {
				return null;
			}
		});
		jsonConfig.registerDefaultValueProcessor(Long.class, new DefaultDefaultValueProcessor() {
			public Object getDefaultValue(Class type) {
				return null;
			}
		});
		net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(result,jsonConfig);
		LOG.error("json:" + json.toString());
		return json.toString();
	}

	/**
	 * 返回失败
	 * @param code 失败编码
	 * @param msg 失败信息
	 * @return Result
	 */
	public static String error(Integer code, String msg) {
		Result result = new Result();
		result.setSuccess(false);
		result.setCode(code);
		result.setMsg(msg);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
		jsonConfig.registerDefaultValueProcessor(Integer.class, new DefaultDefaultValueProcessor() {
			public Object getDefaultValue(Class type) {
				return null;
			}
		});
		jsonConfig.registerDefaultValueProcessor(Long.class, new DefaultDefaultValueProcessor() {
			public Object getDefaultValue(Class type) {
				return null;
			}
		});
		net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(result,jsonConfig);
		LOG.error("json:" + json.toString());
		return json.toString();
	}

	/**
	 * 返回失败
	 * @param msg 失败信息
	 * @return Result
	 */
	public static String error(String msg) {
		Result result = new Result();
		result.setSuccess(false);
		result.setCode(-1);
		result.setMsg(msg);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
		jsonConfig.registerDefaultValueProcessor(Integer.class, new DefaultDefaultValueProcessor() {
			public Object getDefaultValue(Class type) {
				return null;
			}
		});
		jsonConfig.registerDefaultValueProcessor(Long.class, new DefaultDefaultValueProcessor() {
			public Object getDefaultValue(Class type) {
				return null;
			}
		});
		net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(result,jsonConfig);
		LOG.error("json:" + json.toString());
		return json.toString();
	}

	/**
	 * 返回失败
	 * @return Result
	 */
	public static String error() {
		Result result = new Result();
		result.setSuccess(false);
		result.setCode(-1);
		result.setMsg("请求失败");
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
		jsonConfig.registerDefaultValueProcessor(Integer.class, new DefaultDefaultValueProcessor() {
			public Object getDefaultValue(Class type) {
				return null;
			}
		});
		jsonConfig.registerDefaultValueProcessor(Long.class, new DefaultDefaultValueProcessor() {
			public Object getDefaultValue(Class type) {
				return null;
			}
		});
		net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(result,jsonConfig);
		LOG.error("json:" + json.toString());
		return json.toString();
	}

}
